﻿using SharpSoft.Data.GSQL;
using System;
using System.Text;

namespace SharpSoft.Data
{
    /// <summary>
    /// 使用GSQL通用语法的SQL语句，在执行前需要翻译成各自对应的原生语句。（消耗较大。高并发项目或是批量执行大量语句时慎用。）
    /// </summary>
    public class GSQLCommandScript
    {
        public GSQLCommandScript()
        {

        }
        public GSQLCommandScript(string gsqltxt)
        {
            CommandText = gsqltxt;
        }
        public NativeSQLCommandScript ToSql(SQLTextGenerator stg)
        {
            GSQLAnalyzer GLA = new GSQLAnalyzer(this.CommandText);
            var stams = GLA.ReadStatements();
            StringBuilder sb = new StringBuilder();
            foreach (var item in stams)
            {
                sb.Append(stg.ProcessStatement(item))
                    .AppendLine(";");
            }
            return new NativeSQLCommandScript(sb.ToString());
        }

        public string CommandText { get; set; }

        public static implicit operator string(GSQLCommandScript gsql)
        {
            return gsql.CommandText;
        }
        public static explicit operator GSQLCommandScript(string gsqltxt)
        {
            return new GSQLCommandScript() { CommandText = gsqltxt };
        }
    }
}